MongoDB: Schema Validation

You can validate schema for MongoDB forward engineering scripts. The schema validator verifies fields in collections based on their data type. If enabled, this feature restricts forward engineering script generation in case the data in the fields is not in line with the assigned data type. Thus, enabling you to avoid any errors in the forward engineering script. This feature is enabled by default for fields in collections.

In case of any exceptions, you can select the Exclude Schema Validator check box to exclude validations for one or more fields. Doing so overrides the assigned data type of a field and generates the script successfully.

This topic walks you through the steps to generate schema validation for fields in a simple table (collection) and generate schema using an example. The following table lists the fields and assigned data types in a MongoDB collection, Customer.

Fields

Data Type

Name STRING
Year INTEGER
Subjects STRING
Address OBJECT
Street STRING
City STRING

The following image displays the fields in a collection and the assigned data types.

To generate schema validation for a MongoDB collection, follow these steps:

  1. In erwin DM, create a collection with fields, and assign relevant datatypes.

    Ensure that a database in assigned to the collection.

  2. Right-click the collection and click Collection Properties.
    The MongoDB Collection Editor appears.
  3. Go to the Schema Validator tab.
  4. Click to generate schema validation template.
    This displays the generated schema validation template based on the field properties and assigned datatypes.
  5. Click to edit the schema.
    The Collection Schema Validator editor appears. This displays the generated schema validation template with validations in edit mode.
  6. Edit the schema template for validations and add a description for the fields based on your requirement.

    In this example, the validation properties of the fields and descriptions are updated as shown in the following image.

  7. Once you have made changes to the schema template, click OK.
    The schema template validation changes are saved.

  8. On the application ribbon, click Action > Schema.
    The Forward Engineer Schema Generation Wizard appears.

  9. Go to Preview tab.
    The updated schema with the validations appear.

  10. In the Preview section, edit the schema to correct the random integer values.

    Generating schema validation template adds random integer values to the fields with INTEGER datatype. Ensure that you replace this with valid value to generate the schema successfully.

  11. Click Generate.
    The MongoDB Connection screen appears. Connect to the database. For more information, refer to the Database Connection Parameters topic.
    The schema is generated successfully along with the validations.

The schema generation wizard displays errors if the field-level validations are not met. For example, the schema in the following image accepts values between 2020 and 2030 in the Year field. The, Year field is updated with the value 2011.

When you click Generate, the schema generation fails as the validation rules are not met and displays the following error.

Alternatively, you can exclude validations for one or more fields to generate schema. For example, you can exclude schema validation for the field, Subject (STRING), in this collection. Then, replace a string with a number (INTEGER). This overrides the assigned data type of a field and generates the script successfully.

To exclude schema validation for a field, open Collection Field Editor, select a field, and then, select the Exclude Schema Validator check box.

In the Forward Engineering Schema Wizard, when you click Generate, the schema is generated successfully.